Passed
Pull Request — develop (#266)
by
unknown
01:13
created

map.js ➔ ... ➔ ?!?.forEach.call   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 2
b 0
f 1
nc 1
dl 0
loc 13
rs 10
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A map.js ➔ ... ➔ geo.json.forEach 0 11 2
1
define(['map/clientlayer', 'map/labellayer', 'map/button', 'leaflet', 'map/activearea'],
2
  function (ClientLayer, LabelLayer, Button, L) {
3
    'use strict';
4
5
    var options = {
6
      worldCopyJump: true,
7
      zoomControl: true,
8
      minZoom: 0
9
    };
10
11
    return function (linkScale, sidebar, buttons) {
12
      var self = this;
13
      var savedView;
14
15
      var map;
16
      var layerControl;
17
      var baseLayers = {};
18
19
      function saveView() {
20
        savedView = {
21
          center: map.getCenter(),
22
          zoom: map.getZoom()
23
        };
24
      }
25
26
      function contextMenuOpenLayerMenu() {
27
        document.querySelector('.leaflet-control-layers').classList.add('leaflet-control-layers-expanded');
28
      }
29
30
      function mapActiveArea() {
31
        map.setActiveArea({
32
          position: 'absolute',
33
          left: sidebar.getWidth() + 'px',
34
          right: 0,
35
          top: 0,
36
          bottom: 0
37
        });
38
      }
39
40
      function setActiveArea() {
41
        setTimeout(mapActiveArea, 300);
42
      }
43
44
      var el = document.createElement('div');
45
      el.classList.add('map');
46
47
      map = L.map(el, options);
48
      mapActiveArea();
49
50
      var now = new Date();
51
      config.mapLayers.forEach(function (item, i) {
52
        if ((typeof item.config.start === 'number' && item.config.start <= now.getHours()) || (typeof item.config.end === 'number' && item.config.end > now.getHours())) {
53
          item.config.order = item.config.start * -1;
54
        } else {
55
          item.config.order = i;
56
        }
57
      });
58
59
      config.mapLayers = config.mapLayers.sort(function (a, b) {
60
        return a.config.order - b.config.order;
61
      });
62
63
      var layers = config.mapLayers.map(function (d) {
64
        return {
65
          'name': d.name,
66
          'layer': L.tileLayer(d.url.replace('{retina}', L.Browser.retina ? '@2x' : ''), d.config)
67
        };
68
      });
69
70
      map.addLayer(layers[0].layer);
71
72
      layers.forEach(function (d) {
73
        baseLayers[d.name] = d.layer;
74
      });
75
76
      var button = new Button(map, buttons);
77
78
      map.on('locationfound', button.locationFound);
79
      map.on('locationerror', button.locationError);
80
      map.on('dragend', saveView);
81
      map.on('contextmenu', contextMenuOpenLayerMenu);
82
83
      if (config.geo) {
84
        [].forEach.call(config.geo, function (geo) {
85
          geo.json.forEach(function (item) {
86
            if (typeof item === 'function') {
87
              item().then(function (result) {
88
                if (result) {
89
                  L.geoJSON(result, geo.option).addTo(map);
90
                }
91
              });
92
            } else {
93
              L.geoJSON(item, geo.option).addTo(map);
94
            }
95
          });
96
        });
97
      }
98
99
      button.init();
100
101
      layerControl = L.control.layers(baseLayers, [], { position: 'bottomright' });
102
      layerControl.addTo(map);
103
104
      map.zoomControl.setPosition('topright');
105
106
      var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
107
      clientLayer.addTo(map);
108
      clientLayer.setZIndex(5);
109
110
      var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
111
      labelLayer.addTo(map);
112
      labelLayer.setZIndex(6);
113
114
      sidebar.button.addEventListener('visibility', setActiveArea);
115
116
      map.on('zoom', function () {
117
        clientLayer.redraw();
118
        labelLayer.redraw();
119
      });
120
121
      map.on('baselayerchange', function (e) {
122
        map.options.maxZoom = e.layer.options.maxZoom;
123
        clientLayer.options.maxZoom = map.options.maxZoom;
124
        labelLayer.options.maxZoom = map.options.maxZoom;
125
        if (map.getZoom() > map.options.maxZoom) {
126
          map.setZoom(map.options.maxZoom);
127
        }
128
129
        var style = document.querySelector('.css-mode:not([media="not"])');
130
        if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
131
          style.media = 'not';
132
          labelLayer.updateLayer();
133
        }
134
        if (e.layer.options.mode) {
135
          var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
136
          newStyle.media = '';
137
          newStyle.appendChild(document.createTextNode(''));
138
          labelLayer.updateLayer();
139
        }
140
      });
141
142
      map.on('load', function () {
143
        var inputs = document.querySelectorAll('.leaflet-control-layers-selector');
144
        [].forEach.call(inputs, function (input) {
145
          input.setAttribute('role', 'radiogroup');
146
          input.setAttribute('aria-label', input.nextSibling.innerHTML.trim());
147
        });
148
      });
149
150
      var nodeDict = {};
151
      var linkDict = {};
152
      var highlight;
153
154
      function resetMarkerStyles(nodes, links) {
155
        Object.keys(nodes).forEach(function (d) {
156
          nodes[d].resetStyle();
157
        });
158
159
        Object.keys(links).forEach(function (d) {
160
          links[d].resetStyle();
161
        });
162
      }
163
164
      function setView(bounds, zoom) {
165
        map.fitBounds(bounds, { maxZoom: (zoom ? zoom : config.nodeZoom) });
166
      }
167
168
      function goto(m) {
169
        var bounds;
170
171
        if ('getBounds' in m) {
172
          bounds = m.getBounds();
173
        } else {
174
          bounds = L.latLngBounds([m.getLatLng()]);
175
        }
176
177
        setView(bounds);
178
179
        return m;
180
      }
181
182
      function updateView(nopanzoom) {
183
        resetMarkerStyles(nodeDict, linkDict);
184
        var m;
185
186
        if (highlight !== undefined) {
187
          if (highlight.type === 'node' && nodeDict[highlight.o.node_id]) {
188
            m = nodeDict[highlight.o.node_id];
189
            m.setStyle(config.map.highlightNode);
190
          } else if (highlight.type === 'link' && linkDict[highlight.o.id]) {
191
            m = linkDict[highlight.o.id];
192
            m.setStyle(config.map.highlightLink);
193
          }
194
        }
195
196
        if (!nopanzoom) {
197
          if (m) {
198
            goto(m);
199
          } else if (savedView) {
200
            map.setView(savedView.center, savedView.zoom);
201
          } else {
202
            setView(config.fixedCenter);
203
          }
204
        }
205
      }
206
207
      self.setData = function setData(data) {
208
        nodeDict = {};
209
        linkDict = {};
210
211
        clientLayer.setData(data);
212
        labelLayer.setData(data, map, nodeDict, linkDict, linkScale);
213
214
        updateView(true);
215
      };
216
217
      self.resetView = function resetView() {
218
        button.disableTracking();
219
        highlight = undefined;
220
        updateView();
221
      };
222
223
      self.gotoNode = function gotoNode(d) {
224
        button.disableTracking();
225
        highlight = { type: 'node', o: d };
226
        updateView();
227
      };
228
229
      self.gotoLink = function gotoLink(d) {
230
        button.disableTracking();
231
        highlight = { type: 'link', o: d[0] };
232
        updateView();
233
      };
234
235
      self.gotoLocation = function gotoLocation(d) {
236
        button.disableTracking();
237
        map.setView([d.lat, d.lng], d.zoom);
238
      };
239
240
      self.destroy = function destroy() {
241
        button.clearButtons();
242
        sidebar.button.removeEventListener('visibility', setActiveArea);
243
        map.remove();
244
245
        if (el.parentNode) {
246
          el.parentNode.removeChild(el);
247
        }
248
      };
249
250
      self.render = function render(d) {
251
        d.appendChild(el);
252
        map.invalidateSize();
253
      };
254
255
      return self;
256
    };
257
  });
258